package war2100.entity;

import java.io.Serializable;
import java.util.ArrayList;

import java.util.logging.Level;
import java.util.logging.Logger;
import war2100.Editor;
import war2100.Game;
import war2100.Prototypes;
import war2100.entity.help.*;
import war2100.entity.tiles.*;

public class SerializableMap implements Serializable {
	private static final long serialVersionUID = 2193473990214651810L;
	int xSize, ySize;
	@SuppressWarnings("rawtypes")
	private Class[][] sMap;
	private ArrayList<SerializableEntity> entitys;

	public SerializableMap(Map map) {
		xSize = map.getxSize();
		ySize = map.getySize();
		sMap = new Class[xSize][ySize];
		entitys = new ArrayList<SerializableEntity>();

		for (int x = 0; x < xSize; x++) {
			for (int y = 0; y < ySize; y++) {
				sMap[x][y] = map.getField(x, y).getClass();
			}
		}

		for (BasicHelpEntity e : map.getEntitys()) {
			entitys.add(new SerializableEntity(e));
		}
	}

	public Map getMap(Editor editor) {
		Map map = new Map(xSize, ySize, null);

		for (int x = 0; x < xSize; x++) {
			for (int y = 0; y < ySize; y++) {
				// map.setField(x, y, new EarthTile(editor.getPrototypes()));
				Tile t = null;
				if (sMap[x][y].getName() == RockTile.class.getName()) {
					t = new RockTile(editor.getPrototypes());
				}
				if (sMap[x][y].getName() == EarthTile.class.getName()) {
					t = new EarthTile(editor.getPrototypes());
				}
				map.setField(x, y, t);
			}
		}

		for (SerializableEntity e : entitys) {
			map.addEntity(e.getEntity(editor, map));
		}

		return map;
	}

	public Map getMap(Game game) {
		Map map = new Map(xSize, ySize, null);

		for (int x = 0; x < xSize; x++) {
			for (int y = 0; y < ySize; y++) {
				// map.setField(x, y, new EarthTile(editor.getPrototypes()));
				Tile t = null;
				if (sMap[x][y].getName() == null ? RockTile.class.getName() == null : sMap[x][y].getName().equals(RockTile.class.getName())) {
					t = new RockTile(game.getPrototypes());
				}
				if (sMap[x][y].getName() == null ? EarthTile.class.getName() == null : sMap[x][y].getName().equals(EarthTile.class.getName())) {
					t = new EarthTile(game.getPrototypes());
				}
				map.setField(x, y, t);
			}
		}

		for (SerializableEntity e : entitys) {
			map.addEntity(e.getEntity(game, map));
		}

		return map;
	}
}

class SerializableEntity implements Serializable {
	private static final long serialVersionUID = -7598911466291009695L;
	private float x, y;
	@SuppressWarnings("rawtypes")
	private Class eClass;

	public SerializableEntity(BasicHelpEntity e) {
		x = e.getX();
		y = e.getY();
		eClass = e.getClass();
	}

	/**
	 * das ist ne absolute Notloesung und scheisse, das muss man irgendwie ueberarbeiten
	 */
	public BasicHelpEntity getEntity(Prototypes prototypes, Map map) {
		BasicHelpEntity e = null;
		try
		{
			e = (BasicHelpEntity)eClass.getConstructor(new Class[]{float.class, float.class, map.getClass(), prototypes.getClass()}).newInstance(new Object[]{x,y,map,prototypes});
		}catch(Exception ex)
		{
			Logger.getLogger(SerializableEntity.class.getName()).log(Level.SEVERE, ex.getMessage());
		}
//		if (eClass.getName() == null ? PlayerStartPos.class.getName() == null : eClass.getName().equals(PlayerStartPos.class.getName())) {
//			e = new PlayerStartPos(x, y, map, prototypes);
//		}
//		if (eClass.getName() == null ? NestDifficult.class.getName() == null : eClass.getName().equals(NestDifficult.class.getName())) {
//			e = new NestDifficult(x, y, map, prototypes);
//		}
//		if (eClass.getName() == null ? NestNormal.class.getName() == null : eClass.getName().equals(NestNormal.class.getName())) {
//			e = new NestNormal(x, y, map, prototypes);
//		}
//		if (eClass.getName() == null ? NestEasy.class.getName() == null : eClass.getName().equals(NestEasy.class.getName())) {
//			e = new NestEasy(x, y, map, prototypes);
//		}

		return e;
	}

	public BasicHelpEntity getEntity(Editor editor, Map map) {
		return getEntity(editor.getPrototypes(),map);
	}

	public BasicHelpEntity getEntity(Game game, Map map) {
		return getEntity(game.getPrototypes(),map);
	}
}